/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ywb.edugui.view.teacher;

import com.ywb.edugui.controller.ControllerFactory;
import com.ywb.edugui.controller.TeacherController;
import lombok.Data;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.ActionEvent;
import java.util.ResourceBundle;

/**
 *
 * @author 10147
 */
@Data
public class TeacherListPanel extends javax.swing.JPanel {
    private String[] thead = null;
    private String[][] tbody = null;
    private TableModel model = null;
    private TeacherController teacherController = ControllerFactory.getTeacherController(this);

    /**
     * Creates new form TeacherAddOrUpdatePanel
     */
    public TeacherListPanel() {
        initComponents();
    }

    /**
     * 传入新的body或者新head修改table的数据
     * @param body
     * @param head
     */
    public void changeTable(String[][] body, String[] head){
        if(body != null){
            tbody = body;
        }
        if(head != null){
            thead = head;
        }
        model = new DefaultTableModel(tbody, thead);
        jTable1.setModel(model);
    }

    /**
     * 设置组件名称
     */
    public void setRegionText(ResourceBundle bundle) {
        jlQuery.setText(bundle.getString("teacher.querycondition"));
        jlTeacherName.setText(bundle.getString("teacher.name"));
        jlTeacherLevel.setText(bundle.getString("teacher.level"));
        jlTeacherIntro.setText(bundle.getString("teacher.intro"));
        jbQuery.setText(bundle.getString("teacher.query"));
        jbDelete.setText(bundle.getString("teacher.delete"));
        jbUpdate.setText(bundle.getString("teacher.update"));
        jlSort.setText(bundle.getString("teacher.sort"));
        jlPageLimit.setText(bundle.getString("teacher.pagelimit"));
        jbExport.setText(bundle.getString("teacher.exportexcel"));
        jbReset.setText(bundle.getString("teacher.reset"));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jbNext = new javax.swing.JButton();
        jbDelete = new javax.swing.JButton();
        tfTeacherName = new javax.swing.JTextField();
        jlTeacherName = new javax.swing.JLabel();
        jlQuery = new javax.swing.JLabel();
        jlTeacherLevel = new javax.swing.JLabel();
        jlTeacherIntro = new javax.swing.JLabel();
        tfTeacherIntro = new javax.swing.JTextField();
        jbQuery = new javax.swing.JButton();
        cbTeacherLevel = new javax.swing.JComboBox<>();
        jlSort = new javax.swing.JLabel();
        cbSortOrder = new javax.swing.JComboBox<>();
        cbSortCondition = new javax.swing.JComboBox<>();
        jbExport = new javax.swing.JButton();
        jlPageLimit = new javax.swing.JLabel();
        cbPageLimit = new javax.swing.JComboBox<>();
        jbUpdate = new javax.swing.JButton();
        jbPre = new javax.swing.JButton();
        jbReset = new javax.swing.JButton();

        thead = new String[]{"ID","姓名","职称","简介"};
        teacherController.sortList();
        jTable1.setModel(model);
        jScrollPane1.setViewportView(jTable1);

        jbNext.setText("Next");
        jbNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbNextActionPerformed(evt);
            }
        });

        jbDelete.setText("删除教师");
        jbDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDeleteActionPerformed(evt);
            }
        });

        jlTeacherName.setText("教师姓名");

        jlQuery.setText("输入查询条件：");

        jlTeacherLevel.setText("教师职称");

        jlTeacherIntro.setText("教师简介");

        jbQuery.setText("查询");
        jbQuery.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbQueryActionPerformed(evt);
            }
        });

        cbTeacherLevel.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "", "初级教师", "高级教师"}));

        jlSort.setText("排序：");

        cbSortOrder.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "asc", "desc"}));

        cbSortCondition.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] {  "id", "name", "level" }));
        cbSortCondition.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbSortConditionActionPerformed(evt);
            }
        });

        jbExport.setText("导出为Excel");
        jbExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbExportActionPerformed(evt);
            }
        });

        jlPageLimit.setText("分页数：");

        cbPageLimit.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "15", "30", "60"}));

        jbUpdate.setText("修改教师");
        jbUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbUpdateActionPerformed(evt);
            }
        });

        jbPre.setText("Pre");
        jbPre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbPreActionPerformed(evt);
            }
        });

        jbReset.setText("重置");
        jbReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(70, 70, 70)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addComponent(jlSort)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addComponent(cbSortOrder, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(cbSortCondition, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(75, 75, 75)
                                                                .addComponent(jlPageLimit)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addComponent(cbPageLimit, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                .addComponent(jbPre, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addComponent(jbNext, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 715, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addComponent(jlTeacherName)
                                                                        .addGap(29, 29, 29)
                                                                        .addComponent(tfTeacherName, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                        .addComponent(jlTeacherLevel)
                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                        .addComponent(cbTeacherLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                        .addComponent(jlTeacherIntro)
                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                        .addComponent(tfTeacherIntro, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(60, 60, 60)
                                                .addComponent(jlQuery)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jbQuery, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jbDelete, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                        .addComponent(jbExport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jbUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbReset, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                                .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(30, 30, 30)
                                                .addComponent(jlQuery)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jlTeacherName)
                                                        .addComponent(tfTeacherName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jlTeacherLevel)
                                                        .addComponent(jlTeacherIntro)
                                                        .addComponent(tfTeacherIntro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jbQuery, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(cbTeacherLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGap(51, 51, 51)
                                                .addComponent(jbReset, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(30, 30, 30)
                                                .addComponent(jbUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(37, 37, 37)
                                                .addComponent(jbDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jbExport, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jlSort)
                                                        .addComponent(cbSortOrder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(cbSortCondition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jlPageLimit)
                                                        .addComponent(cbPageLimit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(19, 19, 19)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jbNext)
                                                        .addComponent(jbPre))))
                                .addContainerGap(89, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void jbResetActionPerformed(ActionEvent evt) {
        teacherController.resetList();
    }

    private void jbPreActionPerformed(ActionEvent evt) {
        teacherController.prePage();
    }

    private void jbNextActionPerformed(ActionEvent evt) {
        teacherController.nextPage();
    }

    private void jbUpdateActionPerformed(java.awt.event.ActionEvent evt) {
        teacherController.update();
    }

    private void jbDeleteActionPerformed(java.awt.event.ActionEvent evt) {
        teacherController.delete();
    }

    private void jbQueryActionPerformed(java.awt.event.ActionEvent evt) {
        teacherController.query();
    }

    private void cbSortConditionActionPerformed(java.awt.event.ActionEvent evt) {
        teacherController.sortList();
    }

    private void jbExportActionPerformed(java.awt.event.ActionEvent evt) {
        teacherController.exportExcel();
    }


    // Variables declaration - do not modify
    private javax.swing.JComboBox<String> cbPageLimit;
    private javax.swing.JComboBox<String> cbSortCondition;
    private javax.swing.JComboBox<String> cbSortOrder;
    private javax.swing.JComboBox<String> cbTeacherLevel;
    private javax.swing.JButton jbReset;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton jbDelete;
    private javax.swing.JButton jbExport;
    private javax.swing.JButton jbNext;
    private javax.swing.JButton jbPre;
    private javax.swing.JButton jbQuery;
    private javax.swing.JButton jbUpdate;
    private javax.swing.JLabel jlPageLimit;
    private javax.swing.JLabel jlQuery;
    private javax.swing.JLabel jlSort;
    private javax.swing.JLabel jlTeacherIntro;
    private javax.swing.JLabel jlTeacherLevel;
    private javax.swing.JLabel jlTeacherName;
    private javax.swing.JTextField tfTeacherIntro;
    private javax.swing.JTextField tfTeacherName;
    // End of variables declaration
}
